﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PSIMIS2019.Comm;

namespace PSIMIS2019.Templates
{
    public partial class BasePurchaseBillForm : Form
    {
        List<int> selectIndexValue = new List<int>() { 0, 1, 2 };
        int ferryValue = 0;         //摆渡值：针对不同查询条件进行不同操作
        DateTime startD, endD;      //记录查询日期的开始时间、结束时间
        List<string> purchaseIDList;//用于订单编号的列表集合
        string searchStr;           //记录查询字符串

        BasePurchaseBillBLL bl;         //业务逻辑访问类

        //以下是在子类中需要使用到的对象
        protected EmployeeBLL emBLL;
        protected PurchaseBill bo;                          //订单实体类
        protected PurchaseDetail pDetail;                   //详单实体类
        protected List<PurchaseDetail> detailList;          //订单详情泛型集合
        protected DataGridViewTextBoxEditingControl dgvText;//DataGridView文本框控件

        public BasePurchaseBillForm()
        {
            InitializeComponent();
            bl = new BasePurchaseBillBLL();
            ControlInitialState();
        }
        //控件初始状态
        private void ControlInitialState()
        {
            dtpPurchaseDate.Enabled = false;
            cbSupplier.Enabled = false;
            txtOnPress.Enabled = false;
            txtMemo.Enabled = false;

            lbPurchaseBill.Enabled = false;

            txtTotal.Enabled = false;
            cbOnPress.Hide();//隐藏执行状态下拉菜单
        }
        private void cbCondition_SelectedIndexChanged(object sender, EventArgs e)
        {
            ControlInitialState();
            if (selectIndexValue.Contains(cbCondition.SelectedIndex))
            {
                txtSearchString.Show();//显示查询值文本输入框
                cbOnPress.Hide();//隐藏执行状态下拉菜单
            }
            if (cbCondition.SelectedIndex == 3)
            {
                cbOnPress.Show();//显示执行状态下拉菜单
                txtSearchString.Hide();//隐藏查询值文本输入框
            }
            if (cbCondition.SelectedIndex == 4)
            {
                cbOnPress.Hide();//隐藏执行状态下拉菜单
                txtSearchString.Show();//显示查询值文本输入框
            }
        }

        //查询GroupBox的数据验证及查询处理
        private void Valuation()
        {
            List<int> tmpList = new List<int> { 0, 1, 2, 3, 4 };
            if (tmpList.Contains(cbCondition.SelectedIndex))
            {
                ferryValue = cbCondition.SelectedIndex;//将下拉菜单选择下标赋值给摆渡值
                searchStr = txtSearchString.Text.Trim();//获取用户输入的查询字符串  
                  
                if (txtSearchString.TextLength == 0 && selectIndexValue.Contains(ferryValue))
                { MessageBox.Show("请输入需要查询的条件！", "系统提示："); txtSearchString.Focus(); }

                startD = dtpStartDate.Value;//获取开始日期
                endD = dtpEndDate.Value;//获取结束日期
                switch (ferryValue)
                {
                    case 0:
                        purchaseIDList = bl.FindPurchaseBillIDByPurchaseBillID(searchStr);
                        break;
                    case 1:
                        purchaseIDList = bl.FindPurchaseBillIDByClerk(searchStr);
                        break;
                    case 2:
                        purchaseIDList = bl.FindPurchaseBillIDByExaminer(searchStr);
                        break;
                    case 3:
                        purchaseIDList = bl.FindPurchaseBillIDByOnPress(cbOnPress.SelectedIndex);
                        break;
                    case 4:
                        purchaseIDList = bl.FindPurchaseBillIDByDate(startD, endD);
                        break;
                }
            }
            else
            {
                MessageBox.Show("请选择查询条件！", "系统提示：");
            }
        }
        private void btnSearching_Click(object sender, EventArgs e)
        {
            Valuation();//数据校验及查询处理
            if (purchaseIDList.Count!=0)
            {
                lbPurchaseBill.DataSource = purchaseIDList;//将查询结果绑定至订单ID列表
                ClearControls.ClearControl(gbSearch);
            }
            else
                MessageBox.Show("没有符合条件的订单！", "系统提示：");
            lbPurchaseBill.SelectedIndex = -1;//取消订单ID列表的被选状态
            lbPurchaseBill.Enabled = true;
        }


        /// <summary>
        /// 订单完成状态字符串
        /// </summary>
        private string OnPressState(int value)
        {
            string pState = null;
            switch (value)
            {
                case 0:
                    pState = "执行";
                    if (bo.Custodian != null)
                        pState = "执行（已入库）";
                    else if (bo.Examiner != null)
                        pState = "执行（已审核）";
                    else
                        pState = "执行（未审核）";
                    break;
                case 1:
                    pState = "撤销";
                    break;
                case 2:
                    pState = "完成";
                    break;
            }
            return pState;
        }
        private void lbPurchaseBill_Click(object sender, EventArgs e)
        {
            if (lbPurchaseBill.SelectedItem == null)
                MessageBox.Show("请点选具体查询订单！", "系统提示：");
            else
            {
                string purchaseID = lbPurchaseBill.SelectedItem.ToString();//获取ListBox中被选定的订单号
                //根据订单号获取所有订单信息
                bo = bl.FindDetailPurchaseBillByPurchaseBillID(purchaseID);

                if (bo == null)
                    MessageBox.Show("未找到该订单的关联数据。", "系统提示：");
                else
                {
                    dtpPurchaseDate.Value = bo.PurchaseDate;
                    txtPurchaseID.Text = bo.PurchaseBillID;
                    cbSupplier.SelectedValue = bo.SupplierID;//自行处理该Bug
                    txtMemo.Text = bo.Memo;
                    if (bo.StockDate == null)
                        txtStockDate.Text = "等待入库";
                    else
                        txtStockDate.Text = bo.StockDate.ToString();

                    txtOnPress.Text = OnPressState(bo.OnProcess);                    
                }
                detailList = bo.PurchaseDetailList;
                if (detailList.Count == 0)
                    dgvPurchaseDetail.DataSource = null;
                else
                    BindData();//完成详单数据绑定
            }
        }
        /// <summary>
        /// 详单数据绑定
        /// </summary>
        protected void BindData()
        {
            dgvPurchaseDetail.DataSource = null;
            dgvPurchaseDetail.DataSource = detailList;

            dgvPurchaseDetail.Columns[0].DataPropertyName = "PurchaseID";
            dgvPurchaseDetail.Columns[0].HeaderText = "订单编号";
            dgvPurchaseDetail.Columns[0].Width = 100;
            dgvPurchaseDetail.Columns[0].ReadOnly = true;

            dgvPurchaseDetail.Columns[1].DataPropertyName = "PurchaseDetailID";
            dgvPurchaseDetail.Columns[1].HeaderText = "详单编号";
            dgvPurchaseDetail.Columns[1].Width = 100;
            dgvPurchaseDetail.Columns[1].ReadOnly = true;

            dgvPurchaseDetail.Columns[2].DataPropertyName = "ProductID";
            dgvPurchaseDetail.Columns[2].HeaderText = "产品";
            dgvPurchaseDetail.Columns[2].Width = 100;
            dgvPurchaseDetail.Columns[2].ReadOnly = true;

            dgvPurchaseDetail.Columns[3].DataPropertyName = "PurchasePrice";
            dgvPurchaseDetail.Columns[3].HeaderText = "购入价格";
            dgvPurchaseDetail.Columns[3].Width = 100;
            dgvPurchaseDetail.Columns[3].ReadOnly = false;

            dgvPurchaseDetail.Columns[4].DataPropertyName = "Quantity";
            dgvPurchaseDetail.Columns[4].HeaderText = "数量";
            dgvPurchaseDetail.Columns[4].Width = 100;
            dgvPurchaseDetail.Columns[4].ReadOnly = false;

            dgvPurchaseDetail.Columns[5].DataPropertyName = "Account";
            dgvPurchaseDetail.Columns[5].HeaderText = "金额";
            dgvPurchaseDetail.Columns[5].Width = 100;
            dgvPurchaseDetail.Columns[5].ReadOnly = false;

            for(int i = 0; i < detailList.Count; i++)
            {
                var price = Convert.ToDecimal(dgvPurchaseDetail.Rows[i].Cells[3].Value.ToString());//获取对应行的购入价格
                var number= Convert.ToInt32(dgvPurchaseDetail.Rows[i].Cells[4].Value.ToString());//获取对应行的购买数量
                dgvPurchaseDetail.Rows[i].Cells[5].Value = price * number;//计算对应行产品金额
            }
        }

        private void dgvPurchaseDetail_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvPurchaseDetail.CurrentRow.Selected = true;//获取被选行
            pDetail = detailList[e.RowIndex];            //获取被选行的详单信息
            dgvPurchaseDetail.BeginEdit(true);           //明细列表修改状态开始
        }
        private void dgvPurchaseDetail_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < detailList.Count; i++)
            {
                var price = Convert.ToDecimal(dgvPurchaseDetail.Rows[i].Cells[3].Value.ToString());//获取对应行的购入价格
                var number = Convert.ToInt32(dgvPurchaseDetail.Rows[i].Cells[4].Value.ToString());//获取对应行的购买数量
                dgvPurchaseDetail.Rows[i].Cells[5].Value = price * number;//计算对应行产品金额
            }
        }
        private void dgvPurchaseDetail_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 2)
                e.Value = new ProductBLL().FindByID(e.Value.ToString()).ProductName;
        }


    }
}
